//
//  String.swift
//  MedicalBeauty
//
//  Created by Henry on 2018/3/19.
//  Copyright © 2018年 shen_h. All rights reserved.
//

import Foundation
import UIKit

extension String {
    /// 根据固定的size和font计算文字的rect
    ///
    /// - Parameters:
    /// - font: 文字的字体大小
    /// - size: 文字限定的宽高(计算规则:计算宽度, 传入一个实际的高度, 用于计算的宽度则取计算单位的最大值)
    /// - Returns: 返回的CGRect
    func rect(withFont font: UIFont, size: CGSize) -> CGRect {
        return (self as NSString).boundingRect(with: size, options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font: font], context: nil)
    }
    /// 根据固定的size和font计算文字的height
    func height(withFont font: UIFont, size: CGSize) -> CGFloat {
        return self.rect(withFont: font, size: size).height
    }
    /// 根据固定的size和font计算文字的width
    func width(withFont font: UIFont, size: CGSize) -> CGFloat {
        return self.rect(withFont: font, size: size).width
    }
    
    func sizeWithText(font: UIFont, size: CGSize) -> CGRect {
        let attributes = [NSAttributedStringKey.font: font]
        let option = NSStringDrawingOptions.usesLineFragmentOrigin
        let rect:CGRect = self.boundingRect(with: size, options: option, attributes: attributes, context: nil)
        return rect;
    }
    
    
    func setAtturbateString(string:String,textColor:String)->NSAttributedString{
        let atturbateString = NSMutableAttributedString.init(string: self)
        atturbateString.addAttributes([NSAttributedStringKey.foregroundColor : UIColor.init(hexColor: textColor)], range:NSString.init(string: self).range(of: string))
        return atturbateString
    }
    
    
    // MARK:时间字符串转时间戳
    func stringToTimeStamp(timeFormat:String)->String {
        
        let dfmatter = DateFormatter()
        dfmatter.dateFormat = timeFormat//"yyyy年MM月dd日"
        let date = dfmatter.date(from: self)
        
        let dateStamp:TimeInterval = date!.timeIntervalSince1970
        
        let dateSt:Int = Int(dateStamp)
        
        return String(dateSt)
        
    }
    
    
    /// 时间字符串转化为时间
    ///
    /// - Parameter timeFormat: 时间格式
    /// - Returns: date
    func formatDateStringToDate(timeFormat:String)->Date{
        
        let formatter = DateFormatter()
        formatter.dateFormat = timeFormat
        return formatter.date(from: self)!
    }
    
    
    // 时间戳转时间
    func timeStampToString(timeFormat:String)->String {
        
        let string = NSString(string: self)
        
        let timeSta:TimeInterval = string.doubleValue
        
        let dfmatter = DateFormatter()
        
        dfmatter.dateFormat = timeFormat
        
        let date = Date.init(timeIntervalSince1970: timeSta/1000.0)
        
        return dfmatter.string(from: date)
    }
    
//    // MARK:MD5加密
//    func md5String() -> String{
//        let cStr = self.cString(using: String.Encoding.utf8)
//        let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: 16)
//        CC_MD5(cStr!,(CC_LONG)(strlen(cStr!)), buffer)
//        let md5String = NSMutableString();
//        for i in 0 ..< 16{
//            md5String.appendFormat("%02x", buffer[i])
//        }
//        free(buffer)
//        return md5String as String
//    }
    
    func getPriceInteger()->String{
        guard self.contains(".") else {
            return self
        }
        let arr = self.components(separatedBy: ".")
        return arr.first!
    }
    
    var MD5String: String {
        
        let cStrl = cString(using: String.Encoding.utf8)
        
        let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: 16);
        
        CC_MD5(cStrl, CC_LONG(strlen(cStrl!)), buffer)
        
        var md5String = ""
        
        for idx in 0...15 {
            
            let obcStrl = String.init(format: "%02x", buffer[idx])
            
            md5String.append(obcStrl)
            
        }
        free(buffer)
        
        return md5String
        
    }
    
}
